// pages/search/search.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showRecords: true,
    searchHint: '搜索企业名称、所在行业等信息',
    /*搜索历史*/
    searchRecords: null,
    /*企业panel*/
    searchResult: [],
  },
  /*searchBar function*/
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  /*显示|隐藏 搜索历史*/
  showRecords: function () {
    this.setData({
      showRecords: true
    });
  },
  hideRecords: function () {
    this.setData({
      showRecords: false
    });
  },
  /**
   * search
   */
  search: function () {
    var that = this;

    // 查询并得到结果
    wx.request({
      url: app.globalData.server + '/user/company/?keyword=' + that.data.inputVal,
      method: 'GET',
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        'Authorization': 'Bearer ' + app.globalData.token,
        'x-wxapp-signature': null,
        'x-wxapp-timestamp': app.getTimestamp(),
        'x-wxapp-nonce': app.getRandom()
      },
      success: function (res) {
        that.saveRecords(that.data.inputVal);
        console.log(res);
        that.setData({
          searchResult: res.data.data,
        })
        that.hideRecords();
      }
    })
  },
  /**
   * 搜索搜索记录
   */
  searchRecord: function (event) {
    var that = this;
    console.log(event.currentTarget.dataset.inputval);

    that.setData({
      inputVal: event.currentTarget.dataset.inputval,
    })

    // 查询并得到结果
    wx.request({
      url: app.globalData.server + '/user/company/?keyword=' + that.data.inputVal,
      method: 'GET',
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        'Authorization': 'Bearer ' + app.globalData.token,
        'x-wxapp-signature': null,
        'x-wxapp-timestamp': app.getTimestamp(),
        'x-wxapp-nonce': app.getRandom()
      },
      success: function (res) {
        that.saveOldRecords(that.data.inputVal);
        console.log(res);
        that.setData({
          searchResult: res.data.data,
        })
        that.hideRecords();
      }
    })
  },
  /**
   * 缓存搜索记录
   */
  saveRecords: function (inputVal) {
    var that = this;

    var records_list = that.data.searchRecords;

    if (records_list == null) {
      records_list = [];
    }
    if (records_list.length >= 10) {
      console.log("too long");
      for (var i = 9; i > 0; i--) {
        records_list[i] = records_list[i - 1];
      }
    } else {
      records_list.push(" ");
      for (var i = records_list.length - 1; i > 0; i--) {
        records_list[i] = records_list[i - 1];
      }
    }
    records_list[0] = inputVal;
    wx.setStorage({
      key: "searchRecords",
      data: records_list,
    });
  },
  /**
   * 调整搜索记录顺序
   */
  saveOldRecords: function (inputVal) {
    var that = this;

    var records_list = that.data.searchRecords;
    var index;

    for (var i = records_list.length - 1; i > -1; i--) {
      if (records_list[i] == inputVal) {

        index = i;
        console.log(inputVal);
        console.log(index);
        break;
      }
    }
    for (var j = index; j > 0; j--) {
      records_list[j] = records_list[j - 1];
    }

    records_list[0] = inputVal;
    wx.setStorage({
      key: "searchRecords",
      data: records_list,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    // 获得缓存的搜索记录
    wx.getStorage({
      key: 'searchRecords',
      success: function (res) {
        that.setData({
          searchRecords: res.data,
        })
      }
    })

    var category = options.category
    console.log(category);

    if (category != null) {
      this.hideRecords();
      // 获取相关类别的企业
      wx.request({
        url: app.globalData.server + '/user/company/?keyword=' + category,
        method: 'GET',
        data: {},
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
          'Authorization': 'Bearer ' + app.globalData.token,
          'x-wxapp-signature': null,
          'x-wxapp-timestamp': app.getTimestamp(),
          'x-wxapp-nonce': app.getRandom()
        },
        success: function (res) {
          console.log(res);
          that.setData({
            searchResult: res.data.data,
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})